package com.wetao.flexiblerichtextview;

import android.content.Context;
import android.graphics.Color;
import android.os.Handler;
import android.os.Message;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.ScrollView;
import android.widget.TextView;

import com.daquexian.flexiblerichtextview.FlexibleRichTextView;
import com.daquexian.flexiblerichtextview.MyConfige;

import java.util.ArrayList;
import java.util.List;


public class Student02Activity extends AppCompatActivity implements View.OnClickListener, FlexibleRichTextView.OnFlexibleListener{
    private static final String TAG = "Student02";
    private String str00 = "<p><span style=\"font-family: Source Han Sans CN Medium;\">如图所示,终边落在直线</span><span style=\"font-family: Source Han Sans CN Medium;\"><i>y=</i></span><latex>$$ \\sqrt{3} $$</latex><span style=\"font-family: Source Han Sans CN Medium;\"><i>x</i></span><span style=\"font-family: Source Han Sans CN Medium;\">上的角的集合为</span><span style=\"font-family: Source Han Sans CN Medium;\"><i><u> </u></i></span><span style=\"font-family: Source Han Sans CN Medium;\"><u> </u></span><span style=\"font-family: Source Han Sans CN Medium;\"><i><u>  </u></i></span><span style=\"font-family: Source Han Sans CN Medium;\"><i>. </i></span></p><p style=\"text-align:center;text-indent:2.0em;\"><img src=\"http://newpage-edu.oss-cn-beijing.aliyuncs.com/3603d230a0dd46c4a06ca2b83ce4e7f7.jpg\" style=\"width:98px;height:84px;\" /></p>";
    private String str01 = "<p><span style=\"font-family: Times New Roman\"></span><span style=\"font-family: Times New Roman\">(1)</span><span style=\"font-family: Times New Roman\">由题意知</span><span style=\"font-family: Times New Roman\">A=</span><latex>$$ \\sqrt{2} $$</latex><span style=\"font-family: Times New Roman\">,T=4×</span><latex>$$ \\left(\\frac{3\\mathrm{\\pi }}{8}\\text{-}\\frac{\\mathrm{\\pi }}{8}\\right) $$</latex><span style=\"font-family: Times New Roman\">=π,</span></p><p><span style=\"font-family: Times New Roman\">ω=</span><latex>$$ \\frac{2\\mathrm{\\pi }}{\\mathrm{T}} $$</latex><span style=\"font-family: Times New Roman\">=2,</span><span style=\"font-family: Times New Roman\">故</span><span style=\"font-family: Times New Roman\">sin</span><latex>$$ \\left(\\frac{\\mathrm{\\pi }}{8}\\times 2+\\mathrm{\\phi }\\right) $$</latex><span style=\"font-family: Times New Roman\">=1,</span></p><p><span style=\"font-family: Times New Roman\">∴</span><latex>$$ \\frac{\\mathrm{\\pi }}{4} $$</latex><span style=\"font-family: Times New Roman\">+φ=2kπ+</span><latex>$$ \\frac{\\mathrm{\\pi }}{2} $$</latex><span style=\"font-family: Times New Roman\">,k∈Z.</span></p><p><span style=\"font-family: Times New Roman\">∴φ=2kπ+</span><latex>$$ \\frac{\\mathrm{\\pi }}{4} $$</latex><span style=\"font-family: Times New Roman\">,k∈Z.</span></p><p><span style=\"font-family: Times New Roman\">又</span><span style=\"font-family: Times New Roman\">∵φ∈</span><latex>$$ \\left(\\text{-}\\frac{\\mathrm{\\pi }}{2}\\text{,}\\frac{\\mathrm{\\pi }}{2}\\right) $$</latex><span style=\"font-family: Times New Roman\">,∴φ=</span><latex>$$ \\frac{\\mathrm{\\pi }}{4} $$</latex><span style=\"font-family: Times New Roman\">.</span></p><p><span style=\"font-family: Times New Roman\">∴y=</span><latex>$$ \\sqrt{2} $$</latex><span style=\"font-family: Times New Roman\">sin</span><latex>$$ \\left(2\\mathrm{x}+\\frac{\\mathrm{\\pi }}{4}\\right) $$</latex><span style=\"font-family: Times New Roman\">.</span></p><p><span style=\"font-family: Times New Roman\">(2)</span><span style=\"font-family: Times New Roman\">列出</span><span style=\"font-family: Times New Roman\">x</span><span style=\"font-family: Times New Roman\">、</span><span style=\"font-family: Times New Roman\">y</span><span style=\"font-family: Times New Roman\">的对应值表</span><span style=\"font-family: Times New Roman\">:</span></p><table border=\"1\"  cellspacing=\"0\" width=\"261\" ><tr><td><p style=\"text-align:center;\"><span style=\"font-family: Times New Roman\">x</span></p></td><td><p style=\"text-align:center;\"><span style=\"font-family: Times New Roman\">-</span><latex>$$ \\frac{\\mathrm{\\pi }}{8} $$</latex></p></td><td><p style=\"text-align:center;\"><latex>$$ \\frac{\\mathrm{\\pi }}{8} $$</latex></p></td><td><p style=\"text-align:center;\"><latex>$$ \\frac{3\\mathrm{\\pi }}{8} $$</latex></p></td><td><p style=\"text-align:center;\"><latex>$$ \\frac{5\\mathrm{\\pi }}{8} $$</latex></p></td><td><p style=\"text-align:center;\"><latex>$$ \\frac{7\\mathrm{\\pi }}{8} $$</latex></p></td></tr><tr><td><p style=\"text-align:center;\"><span style=\"font-family: Times New Roman\">2x+</span><latex>$$ \\frac{\\mathrm{\\pi }}{4} $$</latex></p></td><td><p style=\"text-align:center;\"><span style=\"font-family: Times New Roman\">0</span></p></td><td><p style=\"text-align:center;\"><latex>$$ \\frac{\\mathrm{\\pi }}{2} $$</latex></p></td><td><p style=\"text-align:center;\"><span style=\"font-family: Times New Roman\">π</span></p></td><td><p style=\"text-align:center;\"><latex>$$ \\frac{3\\mathrm{\\pi }}{2} $$</latex></p></td><td><p style=\"text-align:center;\"><span style=\"font-family: Times New Roman\">2π</span></p></td></tr><tr><td><p style=\"text-align:center;\"><span style=\"font-family: Times New Roman\">y</span></p></td><td><p style=\"text-align:center;\"><span style=\"font-family: Times New Roman\">0</span></p></td><td><p style=\"text-align:center;\"><latex>$$ \\sqrt{2} $$</latex></p></td><td><p style=\"text-align:center;\"><span style=\"font-family: Times New Roman\">0</span></p></td><td><p style=\"text-align:center;\"><span style=\"font-family: Times New Roman\">-</span><latex>$$ \\sqrt{2} $$</latex></p></td><td><p style=\"text-align:center;\"><span style=\"font-family: Times New Roman\">0</span></p></td></tr></table><p><span style=\"font-family: Times New Roman\">描点</span><span style=\"font-family: Times New Roman\">,</span><span style=\"font-family: Times New Roman\">连线</span><span style=\"font-family: Times New Roman\">,</span><span style=\"font-family: Times New Roman\">则</span><span style=\"font-family: Times New Roman\">y=</span><latex>$$ \\sqrt{2} $$</latex><span style=\"font-family: Times New Roman\">sin</span><latex>$$ \\left(2\\mathrm{x}+\\frac{\\mathrm{\\pi }}{4}\\right) $$</latex><span style=\"font-family: Times New Roman\">的图像如图所示</span><span style=\"font-family: Times New Roman\">.</span></p><p style=\"text-align:center;\"><img src=\"http://newpage-edu.oss-cn-beijing.aliyuncs.com/8471819acdbf4aa3a48a707abd956b1d.jpg\" style=\"width:216px;height:167px;\" /></p>";
    private String str02 = "<p><span style=\"font-family: Times New Roman\"></span><span style=\"font-family: Times New Roman\">(1)</span><span style=\"font-family: Times New Roman\">由图像易知</span><span style=\"font-family: Times New Roman\">A=1,</span><span style=\"font-family: Times New Roman\">函数</span><span style=\"font-family: Times New Roman\">f(x)</span><span style=\"font-family: Times New Roman\">的周期为</span><span style=\"font-family: Times New Roman\">T=4×</span><latex>$$ \\left(\\frac{7\\mathrm{\\pi }}{6}\\text{-}\\frac{2\\mathrm{\\pi }}{3}\\right) $$</latex><span style=\"font-family: Times New Roman\">=2π,</span><span style=\"font-family: Times New Roman\">所以</span><span style=\"font-family: Times New Roman\">ω=1,</span><span style=\"font-family: Times New Roman\">由图可知此函数的图像是由</span><span style=\"font-family: Times New Roman\">y=sin x</span><span style=\"font-family: Times New Roman\">的图像沿</span><span style=\"font-family: Times New Roman\">x</span><span style=\"font-family: Times New Roman\">轴负方向平移</span><latex>$$ \\frac{\\mathrm{\\pi }}{3} $$</latex><span style=\"font-family: Times New Roman\">个单位长度得到的</span><span style=\"font-family: Times New Roman\">,</span><span style=\"font-family: Times New Roman\">故</span><span style=\"font-family: Times New Roman\">φ=</span><latex>$$ \\frac{\\mathrm{\\pi }}{3} $$</latex><span style=\"font-family: Times New Roman\">,</span><span style=\"font-family: Times New Roman\">式为</span><span style=\"font-family: Times New Roman\">f(x)=sin</span><latex>$$ \\left(\\mathrm{x}+\\frac{\\mathrm{\\pi }}{3}\\right) $$</latex><span style=\"font-family: Times New Roman\">.</span></p><p><span style=\"font-family: Times New Roman\">(2)</span><span style=\"font-family: Times New Roman\">方程</span><span style=\"font-family: Times New Roman\">f(x)=a</span><span style=\"font-family: Times New Roman\">在</span><latex>$$ \\left(0,\\frac{5\\mathrm{\\pi }}{3}\\right) $$</latex><span style=\"font-family: Times New Roman\">上有两个不同的实根等价于</span><span style=\"font-family: Times New Roman\">y=f(x)</span><span style=\"font-family: Times New Roman\">与</span><span style=\"font-family: Times New Roman\">y=a</span><span style=\"font-family: Times New Roman\">的图像在</span><latex>$$ \\left(0,\\frac{5\\mathrm{\\pi }}{3}\\right) $$</latex><span style=\"font-family: Times New Roman\">上有两个交点</span><span style=\"font-family: Times New Roman\">,</span><span style=\"font-family: Times New Roman\">在同一平面直角坐标系中分别作出</span><span style=\"font-family: Times New Roman\">y=f(x),x∈</span><latex>$$ \\left(0,\\frac{5\\mathrm{\\pi }}{3}\\right) $$</latex><span style=\"font-family: Times New Roman\">与</span><span style=\"font-family: Times New Roman\">y=a</span><span style=\"font-family: Times New Roman\">的图像</span><span style=\"font-family: Times New Roman\">,</span><span style=\"font-family: Times New Roman\">如图所示</span><span style=\"font-family: Times New Roman\">,</span></p><p style=\"text-align:center;\"><img src=\"http://newpage-edu.oss-cn-beijing.aliyuncs.com/84f301d2ad7848dcbefd10c1ea44512d.jpg\" style=\"width:189px;height:105px;\" /></p><p><span style=\"font-family: Times New Roman\">当</span><span style=\"font-family: Times New Roman\">x=0</span><span style=\"font-family: Times New Roman\">时</span><span style=\"font-family: Times New Roman\">,f(x)=</span><latex>$$ \\frac{\\sqrt{3}}{2} $$</latex><span style=\"font-family: Times New Roman\">;</span><span style=\"font-family: Times New Roman\">当</span><span style=\"font-family: Times New Roman\">x=</span><latex>$$ \\frac{5\\mathrm{\\pi }}{3} $$</latex><span style=\"font-family: Times New Roman\">时</span><span style=\"font-family: Times New Roman\">,f(x)=0.</span><span style=\"font-family: Times New Roman\">由图可以看出</span><span style=\"font-family: Times New Roman\">,</span><span style=\"font-family: Times New Roman\">当有两个交点时</span><span style=\"font-family: Times New Roman\">,</span><span style=\"font-family: Times New Roman\">实数</span><span style=\"font-family: Times New Roman\">a</span><span style=\"font-family: Times New Roman\">的取值范围为</span><latex>$$ \\left(\\frac{\\sqrt{3}}{2}\\text{,1}\\right) $$</latex><span style=\"font-family: Times New Roman\">∪(-1,0).</span></p>";

    private static final int HANDLER_FIRST_LOAD = 1;
    private static final int HANDLER_COMPLETE_DELEYED = 2;
    private static final int HANDLER_SHOW_SCROLL_VIEW = 3;
    private static final int HANDLER_SCROLL_BY = 4;

    private boolean isTurnNoneShow = false;  //是否在翻页的隐藏scrollView,然后显示出来（为了肉眼看不出滚动效果，和尝试处理残影）
    private boolean isTurnPage = true;  //是翻页显示还是滚动显示
    private LinearLayout lyt_page_bottom;
    private Button btn_page_prev;
    private Button btn_page_next;
    private TextView tv_page_num;
    private int currentPage = 0;
    private int totalPage = 0;
    private int totalHeight = 0;
    private int currentHeight = 0;
    private ScrollView sv_question;

    private int frtvCount = 0;

    StringBuilder stringBuilder = null;
    FlexibleRichTextView frtv_00 = null;
    FlexibleRichTextView frtv_01 = null;
    FlexibleRichTextView frtv_02 = null;
    List<FlexibleRichTextView> frtvList = new ArrayList<>(); //判断所有的布局都加载完成了，然后显示翻页控件，然后可以对换题操作

    private Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            switch (msg.what) {
                case HANDLER_FIRST_LOAD:{
                    test_01();
                }break;
                case HANDLER_COMPLETE_DELEYED:{
                    currentHeight = sv_question.getHeight();
                    totalHeight = sv_question.getChildAt(0).getHeight();
                    if(totalHeight > currentHeight) {
                        lyt_page_bottom.setVisibility(View.VISIBLE);
                        float f_totalPage = totalHeight/(currentHeight * MyConfige.scroll_by_page_percent);
                        int floor_totalPage = (int)(Math.floor(f_totalPage));
                        if((f_totalPage - floor_totalPage) * (currentHeight * MyConfige.scroll_by_page_percent) > (currentHeight * (1 - MyConfige.scroll_by_page_percent))) {
                            totalPage = floor_totalPage + 1;
                        } else {
                            totalPage = floor_totalPage;
                        }

                        Log.e(TAG, "滚动高度：h1 = " + currentHeight + ", h2 = " + totalHeight + ", f_totalPage = " + f_totalPage + ", totalPage = " + totalPage);
                        refreshPageView();
                    }

                }break;
                case HANDLER_SHOW_SCROLL_VIEW:{
                    sv_question.setVisibility(View.VISIBLE);
                }break;
                case HANDLER_SCROLL_BY:{
                    int by = msg.arg1;
                    sv_question.scrollBy(0, by);
                    refreshPageView();
                    handler.sendEmptyMessageDelayed(HANDLER_SHOW_SCROLL_VIEW, 100);
                }break;
            }
        }
    };
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_student02);

        //禁止上下滚动
        sv_question = (ScrollView)findViewById(R.id.sv_question);
        if(isTurnPage) {
            sv_question.setOnTouchListener(new View.OnTouchListener(){
                @Override
                public boolean onTouch(View arg0, MotionEvent arg1) {
                    return true;
                }
            });
        }

        lyt_page_bottom = (LinearLayout)findViewById(R.id.lyt_page_bottom);
        btn_page_prev = (Button) findViewById(R.id.btn_page_prev);
        btn_page_next = (Button) findViewById(R.id.btn_page_next);
        tv_page_num = (TextView) findViewById(R.id.tv_page_num);

        btn_page_prev.setOnClickListener(this);
        btn_page_next.setOnClickListener(this);


        frtv_00 = (FlexibleRichTextView) findViewById(R.id.frtv_00);
        frtvList.add(frtv_00);
        frtv_00.setOnFlexibleListenerr(this);
        frtv_01 = (FlexibleRichTextView) findViewById(R.id.frtv_01);
        frtvList.add(frtv_01);
        frtv_01.setOnFlexibleListenerr(this);
        frtv_02 = (FlexibleRichTextView) findViewById(R.id.frtv_02);
        frtvList.add(frtv_02);
        frtv_02.setOnFlexibleListenerr(this);


        //必须延时一点时间再加载数据，原因是html解析需要一点时间，所以要界面先显示再加载。
        handler.sendEmptyMessageDelayed(HANDLER_FIRST_LOAD, 50);

    }

    @Override
    protected void onResume() {
        super.onResume();
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.btn_page_prev:{
                currentPage--;
                if(isTurnNoneShow) {
                    sv_question.setVisibility(View.INVISIBLE);
                    Message msg = handler.obtainMessage();
                    msg.what = HANDLER_SCROLL_BY;
                    msg.arg1 = -(int) (currentHeight * MyConfige.scroll_by_page_percent);
                    handler.sendMessageDelayed(msg, 50);
                    //handler.sendEmptyMessageDelayed(HANDLER_SHOW_SCROLL_VIEW, 100);
                } else {
                    sv_question.scrollBy(0, -(int)(currentHeight * MyConfige.scroll_by_page_percent));
                    refreshPageView();
                }


            }break;
            case R.id.btn_page_next:{
                currentPage++;
                if(isTurnNoneShow) {
                    sv_question.setVisibility(View.INVISIBLE);
                    Message msg = handler.obtainMessage();
                    msg.what = HANDLER_SCROLL_BY;
                    msg.arg1 = (int) (currentHeight * MyConfige.scroll_by_page_percent);
                    handler.sendMessageDelayed(msg, 100);
                    //handler.sendEmptyMessageDelayed(HANDLER_SHOW_SCROLL_VIEW, 100);
                } else {
                    sv_question.scrollBy(0, (int)(currentHeight * MyConfige.scroll_by_page_percent));
                    refreshPageView();
                }

            }break;
        }
    }

    @Override
    public void onComplete(FlexibleRichTextView view) {
        Log.i(TAG, "数据全部加载完成");
        frtvCount++;
        if(frtvList.size() == frtvCount) {
            int h1 = sv_question.getHeight();
            int h2 = sv_question.getChildAt(0).getHeight();
            Log.e(TAG, "滚动高度：h1 = " + h1 + ", h2 = " + h2);
            if(h2 > h1 && isTurnPage) {
                handler.sendEmptyMessageDelayed(HANDLER_COMPLETE_DELEYED, 50);
            }
        }

    }

    private void refreshPageView() {
        tv_page_num.setText((currentPage + 1) + "/" + (totalPage == 0 ? 1 : totalPage));
        btn_page_prev.setClickable(false);
        btn_page_next.setClickable(false);
        btn_page_prev.setTextColor(Color.parseColor("#cccccc"));
        btn_page_next.setTextColor(Color.parseColor("#cccccc"));
        if(currentPage > 0) {
            btn_page_prev.setClickable(true);
            btn_page_prev.setTextColor(Color.parseColor("#000000"));
        }
        if((totalPage - 1) > currentPage){
            btn_page_next.setClickable(true);
            btn_page_next.setTextColor(Color.parseColor("#000000"));
        }
    }

    private void test_01() {
        FlexibleRichTextView.load_time = System.currentTimeMillis();

        stringBuilder = new StringBuilder();
        stringBuilder.append(str00);
        frtv_00.setText(stringBuilder.toString());
        stringBuilder = new StringBuilder();
        stringBuilder.append(str01);
        frtv_01.setText(stringBuilder.toString());
        stringBuilder = new StringBuilder();
        stringBuilder.append(str02);
        frtv_02.setText(stringBuilder.toString());
    }
}
